package com.code.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.code.entity.PageResult;
import com.code.entity.QueryPageBean;
import com.code.entity.Result;
import com.code.pojo.Order;
import com.code.pojo.Product;
import com.code.service.OrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {

    @Reference
    OrderService orderService;

    @RequestMapping("editState")
    public Result editState(Integer id){
        orderService.editState(id);
        return new Result();
    }

    @RequestMapping("findProduct")
    public List<Product> findProduct() {
        return orderService.findProduct();
    }

//    @PreAuthorize("hasAnyAuthority('ROLE_ADMIN','ROLE_MY')")
//    @PreAuthorize("hasAnyAuthority('ROLE_MY')")
    @RequestMapping("findPage")
    public PageResult<Order> findPage(@RequestBody QueryPageBean queryPageBean) {
        return orderService.findPage(queryPageBean);
    }

//    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("findPageNo")
    public PageResult<Order> findPageNo(@RequestBody QueryPageBean queryPageBean) {
        return orderService.findPageNo(queryPageBean);
    }


    @RequestMapping("findById")
    public Order findById(@RequestParam Integer id) {
        return orderService.findById(id);
    }

    @RequestMapping("add")
    public Result add(@RequestBody Order order) {
        orderService.add(order);
        return new Result();
    }

    @RequestMapping("update")
    public Result update(@RequestBody Order order) {
        orderService.update(order);
        return new Result();
    }

    @RequestMapping("delete")
    public Result delete(Integer id) {
        orderService.delete(id);
        return new Result();
    }

    @RequestMapping("deleteAll")
    public Result delete(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            orderService.delete(Integer.parseInt(id));
        }
        return new Result();
    }


}
